package com.example.administrator.zxing;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.Manifest;import android.content.Intent;import android.os.Build;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.widget.Toolbar;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.ImageView;import android.widget.TextView;import com.yzq.zxinglibrary.android.CaptureActivity;import com.yzq.zxinglibrary.bean.ZxingConfig;import com.yzq.zxinglibrary.common.Constant;public class MainActivity extends AppCompatActivity { private int REQUEST_CODE_SCAN = 111; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { requestPermissions(new String[] {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_CODE_SCAN); } Button scanBtn=findViewById(R.id.scanBtn); scanBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, CaptureActivity.class); /*ZxingConfig是配置类 可以设置是否显示底部布局,闪光灯,相册,是否播放提示音 震动等动能 * 也可以不传这个参数 * 不传的话 默认都为默认不震动 其他都为true * */ ZxingConfig config = new ZxingConfig(); config.setPlayBeep(true); config.setShake(true); intent.putExtra(Constant.INTENT_ZXING_CONFIG, config); startActivityForResult(intent, REQUEST_CODE_SCAN); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { EditText etc= (EditText) findViewById(R.id.result); super.onActivityResult(requestCode, resultCode, data); // 扫描二维码/条码回传 if (requestCode == REQUEST_CODE_SCAN && resultCode == RESULT_OK) { if (data != null) { String content = data.getStringExtra(Constant.CODED_CONTENT); etc.setText(content); } } }}
评论